package com.leyou.item.web;

import com.leyou.common.dto.PageDTO;
import com.leyou.item.dto.BrandDTO;
import com.leyou.item.service.BrandService;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("/brand")
public class BrandController {

    @Resource
    private BrandService brandService;

    /**
     * 根据id查询品牌
     * @param id 品牌的id
     * @return 品牌对象
     */
    @GetMapping("/{id}")
    public ResponseEntity<BrandDTO> queryBrandById(@PathVariable("id") Long id){

        BrandDTO brandDTO = brandService.queryBrandById(id);
        return ResponseEntity.ok(brandDTO);
    }

    /**
     * 根据品牌id集合查询品牌集合   前端传的是字符串
     * @param ids id集合
     * @return BrandDTO 集合
     */
    @GetMapping("/list")    //@RequestParam("ids") List<Long> ids 前端传的是字符串，spring会自动切割转化为数组
    public ResponseEntity<List<BrandDTO>> queryBrandListByIds(@RequestParam("ids") List<Long> ids){
        List<BrandDTO>brandDTOList = brandService.queryBrandListByIds(ids);
        return ResponseEntity.ok(brandDTOList);
    }

    /**
     * 分页查询
     * @param page 当前页码
     * @param rows 每页大小
     * @param key 查询条件
     * @return 品牌分页结果
     */
    @GetMapping("/page")
    public ResponseEntity<PageDTO<BrandDTO>> queryBrandByPage(
            @RequestParam(value = "page",defaultValue = "1")Integer page,
            @RequestParam(value = "rows",defaultValue = "5")Integer rows,
            @RequestParam(value = "key",required = false)String key){

        PageDTO<BrandDTO> pageDTO = brandService.queryBrandByPage(page,rows, key);
        return ResponseEntity.ok(pageDTO);

    }

    /**
     * 根据分类查询品牌
     * @return 品牌集合
     */
    @GetMapping("/of/category")
    public ResponseEntity<List<BrandDTO>> queryBrandByCategoryId(@RequestParam("id") Long id){
        List<BrandDTO> brandDTOList = brandService.queryBrandByCategoryId(id);
        return ResponseEntity.ok(brandDTOList);
    }

    /**
     * 新增品牌
     * @param brandDTO 品牌对象
     * @return 无
     */
    @PostMapping
    public ResponseEntity<Void> saveBrand(BrandDTO brandDTO) {
        brandService.saveBrand(brandDTO);
        return ResponseEntity.status(HttpStatus.CREATED).build();
    }

    /**
     * 修改品牌
     * @param brandDTO 品牌及分类信息
     * @return 无
     */
    @PutMapping
    public ResponseEntity<Void> updateBrand(BrandDTO brandDTO) {
        brandService.saveBrand(brandDTO);
        return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
    }

    /**
     * 删除品牌
     * @param id 要删除的品牌id
     * @return 无
     */
    @DeleteMapping("/{id}")
    public ResponseEntity<Void> deleteBrandById(@PathVariable("id") Long id){
        // 删除品牌表
        brandService.deleteBrandById(id);
        return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
    }
}
